package com.yitao.cms.service.order;

import com.skipper.base.service.GenericEntityService;
import com.yitao.client.dto.order.OutStockRefundLogDto;
import com.yitao.client.model.order.MallOrderRefundDetailModel;
import com.yitao.client.model.order.OutStockRefundLogModel;
import com.yitao.client.model.order.QOutStockRefundLogModel;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OutStockRefundLogService extends GenericEntityService<OutStockRefundLogModel, OutStockRefundLogDto, QOutStockRefundLogModel> {

    public OutStockRefundLogService(EntityManager entityManager) {
        super(entityManager);
    }

    @Transactional
    public OutStockRefundLogModel save(OutStockRefundLogModel outStockRefundLogModel){
        return repository.save(outStockRefundLogModel);
    }

    /**
     *
     * 功能描述: 新增
     *
     * @param orderId 订单id
     * @param orderDetailId 订单详情id
     * @param redundStatus 退款状态
     * @param refundTradeNo 退款流水
     * @return
     * @auther: Kien
     * @date: 2019/5/15 11:46
     */
    @Transactional
    public OutStockRefundLogModel save(Long orderId, Long orderDetailId, Integer redundStatus, String refundTradeNo){
        if(orderId == null || orderDetailId == null || redundStatus == null){
            return null;
        }
        OutStockRefundLogModel outStockRefundLogModel = new OutStockRefundLogModel();
        outStockRefundLogModel.setOrderId(orderId); // 订单id
        outStockRefundLogModel.setOrderDetailId(orderDetailId); // 订单详情id
        outStockRefundLogModel.setRefundStatus(redundStatus); // 退款状态
        outStockRefundLogModel.setRefundTradeNo(refundTradeNo); // 退款流水号
        outStockRefundLogModel.setIsDelete(false);
        outStockRefundLogModel.setCreateTime(new Date());
        return repository.save(outStockRefundLogModel);
    }

    /**
     *
     * 功能描述:  批量新增
     *
     * @param list 退款申请详情列表
     * @param orderId 订单id
     * @param refundStatus 退款状态
     * @param refundTradeNo 退款流水
     * @return
     * @auther: Kien
     * @date: 2019/5/15 11:46
     */
    @Transactional
    public int saveBatch(List<MallOrderRefundDetailModel> list, Long orderId, Integer refundStatus, String refundTradeNo){
        if(CollectionUtils.isEmpty(list) || orderId == null || refundStatus == null){
            return 0;
        }
        List<OutStockRefundLogModel> outStockRefundLogModelList = new ArrayList<>();
        for(MallOrderRefundDetailModel mallOrderRefundDetailModel: list) {
            OutStockRefundLogModel outStockRefundLogModel = new OutStockRefundLogModel();
            outStockRefundLogModel.setCreateTime(new Date());
            outStockRefundLogModel.setOrderId(orderId); // 订单id
            outStockRefundLogModel.setRefundStatus(refundStatus); // 退款状态
            outStockRefundLogModel.setRefundTradeNo(refundTradeNo); // 退款流水号
            outStockRefundLogModel.setIsDelete(false);
            outStockRefundLogModel.setOrderDetailId(mallOrderRefundDetailModel.getOrderDetailId());
            outStockRefundLogModelList.add(outStockRefundLogModel);
        }
        if(!CollectionUtils.isEmpty(outStockRefundLogModelList)) {
             repository.save(outStockRefundLogModelList);
             return 1;
        }
        return 0;
    }
}
